/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.customerManagement.ejb;

import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.ssau.customerManagement.controller.Controller;
import ru.ssau.customerManagement.controller.CreationException;
import ru.ssau.customerManagement.controller.ServerException;
import ru.ssau.customerManagement.model.entities.Customer;
import ru.ssau.customerManagement.model.entities.CustomerLocation;

/**
 *
 * @author illabefat
 */
@Stateless
@LocalBean
public class ControllerBean implements Controller{
    @PersistenceContext
    protected EntityManager entityManager;
    
    private static final Logger logger = LoggerFactory.getLogger(ControllerBean.class);
    
//    private Controller controller;
    
    public ControllerBean()
    {
    }

    @Override
    public Customer getCustomer(Long id) {        
        Customer entity = entityManager.find(Customer.class, id);
        if (entity == null)
        {
            throw new ServerException("customer not found");
        }
        return entity;
    }

    @Override
    public List<Customer> getCustomers() {
        List<Customer> customers = entityManager.createNamedQuery("Customer.allCustomers").getResultList();
        return customers;
    }

    @Override
    public List<Customer> findCustomersByName(String name) {
        List<Customer> customers = entityManager.createNamedQuery("Customer.byName").setParameter("name", "%" + name + "%").getResultList();
        return customers;
    }

    @Override
    public void addCustomer(Customer customer) throws CreationException {
        Customer customerFromDB = entityManager.find(Customer.class, customer.getId());
        if (customerFromDB != null)
        {
            throw new CreationException("Custoemr with the specified id already exists");
        }
        entityManager.persist(customer);
        entityManager.flush();
    }

    @Override
    public void replaceCustomer(Customer newCustomer) {        
        Customer customerFromDB = entityManager.find(Customer.class, newCustomer.getId());
        if (customerFromDB == null)
        {
            throw new ServerException("Customer with the specified id does not exist");
        }
        customerFromDB.setName(newCustomer.getName());
        customerFromDB.setDescription(newCustomer.getDescription());
        entityManager.persist(customerFromDB);
        entityManager.flush();
    }

    @Override
    public void deleteCustomer(Long customerId) {        
        Customer customer = entityManager.find(Customer.class, customerId);
        if (customer == null)
        {
            throw new ServerException("Customer with the specified id does not exist");
        }
        entityManager.remove(customer);
        entityManager.flush();
    }

    @Override
    public CustomerLocation getLocation(Long id) {
        CustomerLocation entity = entityManager.find(CustomerLocation.class, id);
        if (entity == null)
        {
            throw new RuntimeException("customer not found");
        }
        return entity;
    }

    @Override
    public List<CustomerLocation> getLocationsByCustomer(Long customerId) {
        List<CustomerLocation> customerLocations = entityManager.createNamedQuery("CustomerLocation.byCustomer").setParameter("customer_id", customerId).getResultList();
        return customerLocations;
    }

    @Override
    public List<CustomerLocation> findCustomerLocationsByAddress(String address) {
        List<CustomerLocation> customerLocations = entityManager.createNamedQuery("CustomerLocation.byAddress").setParameter("address", "%" + address + "%").getResultList();
        return customerLocations;
    }

    @Override
    public List<CustomerLocation> getAllLocations() {
        List<CustomerLocation> customerLocations = entityManager.createNamedQuery("CustomerLocation.allLocations").getResultList();
        return customerLocations;
    }  

    @Override
    public void addCustomerLocation(CustomerLocation location) throws CreationException {
        CustomerLocation customerLocationFromDB = entityManager.find(CustomerLocation.class, location.getId());
        if (customerLocationFromDB != null)
        {
            throw new CreationException("CustomerLocation with the specified id already exists");
        }
        entityManager.persist(location);
        entityManager.flush();
    }

    @Override
    public void replaceCustomerLocation(CustomerLocation location) {
        CustomerLocation customerLocationFromDB = entityManager.find(CustomerLocation.class, location.getId());
        if (customerLocationFromDB == null)
        {
            throw new ServerException("CustomerLocation with the specified id does not exist");
        }
        customerLocationFromDB.setAddress(location.getAddress());
        entityManager.persist(customerLocationFromDB);
        entityManager.flush();
    }

    @Override
    public void deleteCustomerLocation(Long customerLocationId) {
        CustomerLocation customerLocation = entityManager.find(CustomerLocation.class, customerLocationId);
        if (customerLocation == null)
        {
            throw new ServerException("CustomerLocation with the specified id does not exist");
        }
        entityManager.remove(customerLocation);
        entityManager.flush();
    }
}
